/*
 * Copyright 2010 Mttang.com All right reserved. This software is the confidential and proprietary information of
 * Mttang.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Mttang.com.
 */
package com.mttang.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;

/**
 * 类DateUtil.java的实现描述：TODO 类实现描述
 * 
 * @author ydliao 2011-2-22 上午11:22:20
 */
public class DateUtil extends DateUtils {

    private final static Map<String, SimpleDateFormat> dateFormatMap = new HashMap<String, SimpleDateFormat>();

    // 将日期转换为字符串
    public static String date2String(Date date, String pattern) {
        SimpleDateFormat dateFormat = dateFormatMap.get(pattern);
        if (null == dateFormat) {
            dateFormat = new SimpleDateFormat(pattern);
            dateFormatMap.put(pattern, dateFormat);
        }

        return dateFormat.format(date);
    }

    // 将字符串转换为日期
    public static Date string2Date(String strDate, String pattern) throws ParseException {
        if (strDate == null) {
            return null;
        }
        SimpleDateFormat dateFormat = dateFormatMap.get(pattern);
        if (null == dateFormat) {
            dateFormat = new SimpleDateFormat(pattern);
            dateFormatMap.put(pattern, dateFormat);
        }

        return dateFormat.parse(strDate);
    }
}
